package com.dataseek.iot.core.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.request.QueryPage2;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;

/**
 * @author cc
 * @Title: 解析json
 * @Description: 解析json
 * @date 2021/9/1710:01 上午
 * @since 1.0
 */
@Slf4j
public class ParseUtil {


    public static QueryWrapper parseJson(JSONObject json){

        QueryWrapper wrapper = Wrappers.query();

        json.forEach(new BiConsumer<String, Object>() {
            @Override
            public void accept(String s, Object o) {

                if (Objects.nonNull(s) && Objects.nonNull(o))
                    switch (s) {
                        case "%line_code":
                            String str = (String) o;
                            String[] codes = str.split(",");
                            for (int i = 0; i < codes.length; i++) {
                                if (i == 0) {
                                    wrapper.like("line_code", codes[0]);
                                } else {
                                    wrapper.or(true).like("line_code", codes[i]);
                                }
                            }
                            break;
                        default:
                            wrapper.eq(s, o);
                    }
            }
        });
        return wrapper;
    }

    public static QueryPage2 parseJsonWithPage(JSONObject json){
        QueryPage2 query = new QueryPage2();
        QueryWrapper wrapper =Wrappers.query();
        Page p = new Page();

        json.forEach(new BiConsumer<String, Object>() {
            @Override
            public void accept(String s, Object o) {
                if(Objects.nonNull(s)&&Objects.nonNull(o)){
                    if(contains(s, "date", "time")){
                        List duration = (List<Object>)o;
                        Object startTime = duration.get(0);
                        Object endTime = duration.get(1);
                        if(startTime.equals(endTime)){
                            wrapper.eq(s, startTime);
                        }else {
                            wrapper.between(s, duration.get(0),duration.get(1));
                        }
                    }else {
                        switch (s){
                            case "current":
                                p.setCurrent(Long.valueOf(o.toString()));
                                break;
                            case "size":
                                p.setSize(Long.valueOf(o.toString()));
                                break;
                            case "asc":
                                p.setAscs((List<String>) o);
                                break;
                            case "desc":
                                p.setDescs((List<String>)o);
                                break;
                            case "line_code":
                                wrapper.like("line_code", o);
                                break;
                            case "%device_code":
                                String str = (String)o;
                                String[] codes = str.split(",");
                                wrapper.in("device_code", codes);
                                break;
                            default:
                                wrapper.eq(s, o);
                        }
                    }
                }
            }
        });
        log.info("获取的sql为："+ wrapper.getTargetSql());
        query.setPage(p);
        query.setWrapper(wrapper);
        return query;
    }

    private static Boolean contains(String mainStr, String... subStrs){
        for(String s: subStrs){
            if (mainStr.contains(s)) return true;
        }
        return false;
    }

    private static void assemblyLikeSql(QueryWrapper<Wrapper> wrapper, String[] codes, String column){
        Integer len = codes.length;
        switch (len){
            case 1:
                wrapper.like(column,codes[0]);
                break;
            case 2:
                wrapper.like(column, codes[0])
                        .or(true)
                        .like(column, codes[1]);
                break;
            case 3:
                wrapper.like(column, codes[0])
                        .or(true)
                        .like(column, codes[1])
                        .or(true)
                        .like(column, codes[2]);
                break;
            case 4:
                wrapper.like(column, codes[0])
                        .or(true)
                        .like(column, codes[1])
                        .or(true)
                        .like(column, codes[2])
                        .or(true)
                        .like(column, codes[3]);
                break;
            case 5:
                wrapper.like(column, codes[0])
                        .or(true)
                        .like(column, codes[1])
                        .or(true)
                        .like(column, codes[2])
                        .or(true)
                        .like(column, codes[3])
                        .or(true)
                        .like(column, codes[4]);
                break;
            case 6:
                wrapper.like(column, codes[0])
                        .or(true)
                        .like(column, codes[1])
                        .or(true)
                        .like(column, codes[2])
                        .or(true)
                        .like(column, codes[3])
                        .or(true)
                        .like(column, codes[4])
                        .or(true)
                        .like(column, codes[5]);
                break;
            case 7:
                wrapper.like(column, codes[0])
                        .or(true)
                        .like(column, codes[1])
                        .or(true)
                        .like(column, codes[2])
                        .or(true)
                        .like(column, codes[3])
                        .or(true)
                        .like(column, codes[4])
                        .or(true)
                        .like(column, codes[5])
                        .or(true)
                        .like(column, codes[6]);
                break;
            case 8:
                wrapper.like(column, codes[0])
                        .or(true)
                        .like(column, codes[1])
                        .or(true)
                        .like(column, codes[2])
                        .or(true)
                        .like(column, codes[3])
                        .or(true)
                        .like(column, codes[4])
                        .or(true)
                        .like(column, codes[5])
                        .or(true)
                        .like(column, codes[6])
                        .or(true)
                        .like(column, codes[7]);
                break;
            case 9:
                wrapper.like(column, codes[0])
                        .or(true)
                        .like(column, codes[1])
                        .or(true)
                        .like(column, codes[2])
                        .or(true)
                        .like(column, codes[3])
                        .or(true)
                        .like(column, codes[4])
                        .or(true)
                        .like(column, codes[5])
                        .or(true)
                        .like(column, codes[6])
                        .or(true)
                        .like(column, codes[8]);
                break;
            default:
                break;
        }
    }

}
